import settings from "../utils/settings";
import { MessageID } from "../utils/utils";

if(!FileLib.exists("./config/ChatTriggers/modules/BabyzombieAddons/config/note.TXT")) {
    FileLib.append("./config/ChatTriggers/modules/BabyzombieAddons/config/note.TXT","开游戏和/ct reload时显示一次   >>开游戏和/ct reload时不显示<<\r\n这是默认的第一行")
} //如果没有 note.TXT 就试着生成一个

const BZANoteFile = "./config/ChatTriggers/modules/BabyzombieAddons/config/note.TXT";

function arrayshift(array) { return array.splice(1,array.length-1); }

export let BZANote = {
    ShowOnGameLoad: FileLib.read(BZANoteFile).split('\r\n')[0],
    text: arrayshift(FileLib.read(BZANoteFile).split('\r\n')),
    save() { FileLib.write(BZANoteFile,this.ShowOnGameLoad + '\r\n' + this.text.join('\r\n')); },
    show() {
        if(this.text[0]){
            ChatLib.chat("§5§m                 §6§l BZANote §5§m                 ")
            this.text.forEach(e => ChatLib.chat(e));
            ChatLib.chat("§5§m                                            ")
        }
    },
    togglefirstshow() {
        if(this.ShowOnGameLoad == ">>开游戏和/ct reload时显示一次<<   开游戏和/ct reload时不显示") {
            this.ShowOnGameLoad = "开游戏和/ct reload时显示一次   >>开游戏和/ct reload时不显示<<";
            ChatLib.chat("§7[§6§lBZANote§7] §5开游戏和/ct reload时 §c不显示");
        }else{
            this.ShowOnGameLoad = ">>开游戏和/ct reload时显示一次<<   开游戏和/ct reload时不显示";
            ChatLib.chat("§7[§6§lBZANote§7] §5开游戏和/ct reload时 §c显示一次");
        }
        this.save();
    },
    firstshow() {
        let first = register("worldLoad",() => {
            if(this.ShowOnGameLoad == ">>开游戏和/ct reload时显示一次<<   开游戏和/ct reload时不显示") setTimeout(() => {this.show()}, 3000); 
            first.unregister();
        })
    },
    add(text,start) {
        this.text.splice(start-1,0,text);
        ChatLib.chat("§7[§6§lBZANote§7] §5在第 §e" + start + " §5行添加了: \n§r" + text);
        this.save();
    },
    del(start) {
        let thistext = this.text[start-1]
        this.text.splice(start-1,1);
        ChatLib.chat("§7[§6§lBZANote§7] §5删除了位于第 §e" + start + " §5行的: \n§r" + thistext);
        this.save();
    },
    delall() {
        this.text.splice(0,this.text.length);
        ChatLib.chat("§7[§6§lBZANote§7] §5清除了笔记内的所有内容");
        this.save();
    },
    change(text,start) {
        this.text.splice(start-1,1,text);
        ChatLib.chat("§7[§6§lBZANote§7] §5成功修改了第 §e" + start + " §5行的内容为: \n§r" + text);
        this.save();
    }
}

const NoteHelp = new Message(
    new TextComponent("§5§m                    §6§l  BZANote §7- §bhelp  §5§m                    \n").setHover("show_text","§6这是第一行"),
    new TextComponent("§e/bza note add (内容) <行数>     §b在笔记中添加一行在指定位置")
    .setClick("suggest_command",`/${settings.modulecmd} note add `),'\n',
    new TextComponent("§e/bza note del <行数>            §b在笔记中删除指定位置的那行")
    .setClick("suggest_command",`/${settings.modulecmd} note del `),'\n',
    new TextComponent("§e/bza note delall                §b清除笔记中所有内容")
    .setClick("run_command",`/${settings.modulecmd} note delall`).setHover("show_text","§c点击后会执行一次清除.此操作不可撤销!!!"),'\n',
    new TextComponent("§e/bza note set (内容) (行数)  §b修改指定那行的内容")
    .setClick("suggest_command",`/${settings.modulecmd} note set `),'\n',
    new TextComponent("§e/bza note list                  §b查看目前笔记内容")
    .setClick("run_command",`/${settings.modulecmd} note list`).setHover("show_text","§a点击查看当前已有的所有内容"),'\n',
    new TextComponent("§e/bza note ongameload            §b开关游戏启动和/ct reload时的显示")
    .setClick("run_command",`/${settings.modulecmd} note ongameload`).setHover("show_text","§a点击开关 启动游戏时显示"),'\n',
    new TextComponent("§5§m                                                           ").setHover("show_text","§6这是最后一行")
).setChatLineId(MessageID.Help);

export function note(cmd="help",args=[]) {
    let num = (args[args.length - 1].replace(/[1-9]/g,"").length == 0 ? parseInt(args[args.length - 1]): 0);
    let notetext = (args[2] ? (args.splice(2,args.length-2)).join(" ") : "");
    switch (cmd) {
        case "list":
            BZANote.show();
            break;
        case "ongameload":
            BZANote.togglefirstshow();
            break;
        case "a":
        case "add":
        case "new":
            if(num) {
                BZANote.add(notetext.substring(0,notetext.length - (num.toString().length + 1)) , num)
            } else {
                BZANote.add(notetext,BZANote.text.length + 1);
            }
            break;
        case "d":
        case "del":
        case "remove":
            if(num && num <= BZANote.text.length) {
                BZANote.del(num);
            } else {
                BZANote.del(BZANote.text.length);
            }
            break;
        case "delall":
            BZANote.delall();
            break;
        case "edit":
        case "set":
        case "change":
            if(num && num <= BZANote.text.length) {
                BZANote.change(notetext.substring(0,notetext.length - (num.toString().length + 1)) , num)
            };
            break;
        default:
            NoteHelp.chat();
            break;
    }
}